name: Call integration of flagship Apps

# see: https://help.github.com/en/actions/reference/events-that-trigger-workflows
on:
  workflow_call:
    inputs:
      environment:
        description: "Lightning environment"
        required: false
        default: "PROD"
        type: string
  workflow_dispatch:
    inputs:
      environment:
        description: "Lightning environment"
        required: true
        default: "PROD"
        type: choice
        options:
          - PROD
          - STAGING

defaults:
  run:
    shell: bash

jobs:
  run-flagships:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/playwright/python:v1.38.0-focal
    strategy:
      fail-fast: false
      matrix:
        include:
          - { app: "flashy", repo: "Lightning-Universe/Flashy-app" }
          - { app: "muse", repo: "Lightning-Universe/stable-diffusion-deploy" }
          - { app: "jupyter", repo: "Lightning-Universe/Jupyter-component" }

          # TODO:
          # - Training Studio
          # - Echo
          # - StreamLit / Gradio
          # - All homepage & docs apps

    env:
      HEADLESS: "1"
      PACKAGE_LIGHTNING: "1"
      CLOUD: "1"
      VIDEO_LOCATION: "./videos"
      HAR_LOCATION: "./artifacts/hars"
      SLOW_MO: "50"
      LIGHTNING_DEBUG: "1"
      TORCH_URL: "https://download.pytorch.org/whl/cpu/torch_stable.html"
    # Timeout: https://stackoverflow.com/a/59076067/4521646
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v4

      - name: basic setup
        timeout-minutes: 20
        run: |
          mkdir -p tests/_flagships
          mkdir -p $VIDEO_LOCATION
          pip --version
          pip list
          # for some reason the python package playwright is missing
          pip install -r requirements/app/test.txt
          python -m playwright install  # --with-deps

      - name: Clone the Repo/App
        uses: actions/checkout@v4
        with:
          repository: ${{ matrix.repo }}
          path: tests/_flagship-app

      - name: Adjust env. for this App
        uses: ./.github/actions/prep-apps
        with:
          name: ${{ matrix.app }}

      - name: Install Lightning package
        timeout-minutes: 20
        run: pip install -e .[cloud,test] -f $TORCH_URL
      - name: List pip dependency
        run: pip --version && pip list

      - name: Run the tests
        working-directory: tests/
        env:
          LIGHTNING_USER_ID: ${{ secrets[format('LIGHTNING_USER_ID_{0}', inputs.environment)] }}
          LIGHTNING_API_KEY: ${{ secrets[format('LIGHTNING_API_KEY_{0}', inputs.environment)] }}
          LIGHTNING_USERNAME: ${{ secrets[format('LIGHTNING_USERNAME_{0}', inputs.environment)] }}
          LIGHTNING_CLOUD_URL: ${{ secrets[format('LIGHTNING_CLOUD_URL_{0}', inputs.environment)] }}
          LAI_USER: ${{ secrets.LAI_SSH_USER }}
          LAI_PASS: ${{ secrets.LAI_SSH_PASS }}
        run: |
          ls -l _flagship-app
          python -m pytest integrations_app/flagship/test_${{ matrix.app }}.py \
            --capture=no -v --color=yes

      - name: Upload recordings
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: flahship-app-${{ matrix.app }}
          path: ${{ env.VIDEO_LOCATION }}

      - name: Clean Previous Apps
        if: always()
        timeout-minutes: 3
        env:
          LIGHTNING_USER_ID: ${{ secrets[format('LIGHTNING_USER_ID_{0}', inputs.environment)] }}
          LIGHTNING_API_KEY: ${{ secrets[format('LIGHTNING_API_KEY_{0}', inputs.environment)] }}
          LIGHTNING_USERNAME: ${{ secrets[format('LIGHTNING_USERNAME_{0}', inputs.environment)] }}
          LIGHTNING_CLOUD_URL: ${{ secrets[format('LIGHTNING_CLOUD_URL_{0}', inputs.environment)] }}
          LAI_USER: ${{ secrets.LAI_SSH_USER }}
          LAI_PASS: ${{ secrets.LAI_SSH_PASS }}
        run: |
          time python -c "from lightning.app import testing; testing.delete_cloud_lightning_apps()"
